package com.lzw.breezeNovel.webApp.service.impl.comic;

import com.lzw.breezeNovel.common.exception.BaseException;
import com.lzw.breezeNovel.common.login.LoginUser;
import com.lzw.breezeNovel.common.login.LoginUserHolder;
import com.lzw.breezeNovel.common.result.ResultCodeEnum;
import com.lzw.breezeNovel.model.entity.pojo.comic.ComicRating;
import com.lzw.breezeNovel.model.entity.pojo.user.UserInfo;
import com.lzw.breezeNovel.webApp.mapper.comic.ComicMapper;
import com.lzw.breezeNovel.webApp.mapper.comic.ComicRatingMapper;
import com.lzw.breezeNovel.webApp.mapper.user.UserInfoMapper;
import com.lzw.breezeNovel.webApp.service.comic.ComicRatingService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.comic
 * @date 2025/3/17
 * @description 漫画评分
 */
@Slf4j
@Service
@SuppressWarnings("all")
public class ComicRatingServiceImpl  implements ComicRatingService {

    /**
     * 注入用户mapper
     */
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 注入评分mapper
     */
    @Autowired
    private ComicRatingMapper comicRatingMapper;
    /**
     * 注入漫画mapper
     */
    @Autowired
    private ComicMapper comicMapper;

    /**
     * 漫画评分
     * @param comicId
     * @param rating
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rating(Long comicId, Double rating) {
        Long userId = getCurrentUser();
        // 检查用户是否已经评分
        if (comicRatingMapper.selectByUserIdAndComicId(userId, comicId)) {
            throw new BaseException(ResultCodeEnum.COMIC_RATING_OVER);
        }
        // 创建评分对象
        ComicRating comicRating = createRating(userId, comicId, rating);
        // 保存评分
        comicRatingMapper.insert(comicRating);
        // 更新漫画评分
        updateComicRating(comicId, rating);
    }

    /**
     * 创建评分对象
     * @param userId 用户ID
     * @param comicId 漫画ID
     * @param rating 评分
     * @return 评分对象
     */
    @NotNull
    private ComicRating createRating(Long userId, Long comicId, Double rating) {
        ComicRating comicRating = new ComicRating();
        comicRating.setUserId(userId);
        comicRating.setComicId(comicId);
        comicRating.setRating(rating);
        return comicRating;
    }

    /**
     * 更新漫画评分
     * @param comicId 漫画ID
     * @param rating 评分
     */
    private void updateComicRating(Long comicId, Double rating) {
        Integer count = comicMapper.updateComic(comicId, rating);
        // 检查更新是否成功
        if (count == 0) {
            throw new BaseException(500, "漫画评分更新失败");
        }
    }

    /**
     * 根据用户id判断是否评分了
     * @param comicId
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public Double getRatingByUserId(Long comicId) {
        //获取当前用户
        Long userId = getCurrentUser();
        //根据用户id和漫画id查询评分
        ComicRating rating = comicRatingMapper.selectRateingByUserIdAndComicId(userId,comicId);
        if(rating == null){
            return 0.0;
        }
        return rating.getRating();
    }

    /**
     * 获取当前用户
     */
    public Long getCurrentUser() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断用户是否登录
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        String phone = loginUser.getPhone();
        //获取用户信息
        UserInfo userInfo = userInfoMapper.selectByPhone(phone);
        if (userInfo == null) {
            throw new BaseException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        return userInfo.getUserId();
    }
}
